import React, {Component, Fragment} from 'react';
import {reqBookMessage} from '../../api/book';
import {Button, Card, Table, Image, message} from 'antd';
import {PlusOutlined} from "@ant-design/icons";
import {Link} from "react-router-dom";
import {dateTimeDateFormat} from "../../utils/tools";

// 图书通知
export default class BookMessage extends Component{
  state = {
    result: [],
    totalCount: 0,
  }
  // 删除
  deleteClick = () => {

  }
  // 初始化
  initColumns = () => {
    this.columns = [
      {
        title: '标题',
        dataIndex: 'title',
        render: (text, record) => <Link to={'/admin/books/messages/'+record._id}>{record.title}</Link>
      },
      {
        title: '作者',
        dataIndex: 'author',
        key: 'author',
      },
      {
        title: '描述',
        dataIndex: 'slug',
        key: 'slug',
      },
      {
        title: '封面图',
        dataIndex: 'image',
        render: (image => <Image src={image} width={100}/>)
      },
      {
        title: '创建时间',
        dataIndex: 'add_time',
        render: (add_time => dateTimeDateFormat(add_time))
      },
      {
        title: '删除',
        dataIndex: '_id',
        render: (text, record) => <Button type='primary' danger onClick={this.deleteClick}>删除</Button>
      }
    ]
  }

  UNSAFE_componentWillMount() {
    this.initColumns();
  }

  // 获得图书通知
  getBookMessage = async () => {
    try {
      const {data: result} = await reqBookMessage();
      this.setState({result: result.items, totalCount:result.total_count});
    } catch (e) {
      message.error(e.data.name || e.data.message);
    }
  }

  componentDidMount() {
    this.getBookMessage();
  }

  render () {
    const {result, totalCount} = this.state;

    const extra = (
      <Fragment>
        <Button type='primary' onClick={this.addClick} icon={<PlusOutlined/>} style={{marginRight: '10px'}}>
          添加
        </Button>
      </Fragment>
    )
    const pagination = {
      defaultPageSize: 5,
      position: ['bottomCenter'],
      showQuickJumper: true,
      total: totalCount,
    }

    return (
      <Card title='图书资讯' extra={extra}>
        <Table dataSource={result}
               rowKey='_id'
               scroll={{x: 800}}
               pagination={pagination}
               columns={this.columns}
        />
      </Card>
    )
  }
}
